package com.example.springboot.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public  class EmailUtil {
    private final static String from="510830970@qq.com";
    private final static String authPwd="gekiohltyqhobibd";

    /**
     * 发送文本邮件
     * @param to 收件人邮箱
     * @param text 邮件文本内容
     * @param title 邮件标题
     * @author 胡俊
     */
   public static  void send(String to,String text,String title) {
        Properties properties = System.getProperties();
        // 设置邮件服务器
        setProperties(properties);
        // 获取默认session对象
        Session session = getSession(from, properties);
        try{
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(title);
            message.setText(text);
            Transport.send(message);
        }catch (Exception mex) {

            mex.printStackTrace();
        }
    }

    /**
     * 收邮件方法
     */
    public static void revice(){
        try {
            String host = "pop.qq.com";
            String username = "510830970@qq.com";
            String password = "gekiohltyqhobibd";
            Properties p = new Properties();
            p.setProperty("mail.pop3.host", "pop.qq.com"); // 按需要更改
            p.setProperty("mail.pop3.port", "995");
            // SSL安全连接参数
            p.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.setProperty("mail.pop3.socketFactory.fallback", "true");
            p.setProperty("mail.pop3.socketFactory.port", "995");
            Session session = Session.getDefaultInstance(p, null);
            Store store = session.getStore("pop3");
            store.connect(host, username, password);
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message message[] = folder.getMessages();
            new GetMailInfoThread(message).start();
        }
        catch (NoSuchProviderException e)
        {
            e.printStackTrace();
        }
        catch (MessagingException e)
        {
            e.printStackTrace();
        }
    }

    private static Session getSession(String from, Properties properties) {
        return Session.getDefaultInstance(properties,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(from, authPwd); //发件人邮件用户名、密码
                }
            });
    }

    private static void setProperties(Properties properties) {
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.port", "465");
    }


    public static void main(String[] args) {
//        EmailUtil.revice();
        EmailUtil.send("1397848054@qq.com","nihao","tit");
    }
}
